package com.cerosoft.components;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

import com.cerosoft.api.Config;
import com.cerosoft.utils.IndexChangeListener;

public class DynamicWebView extends WebView implements IndexChangeListener {

	private List<String> webFiles = null;

	private int currIndex = -1;

	private String ipAddr = null;

	private void getViewByIndex() {
		if (webFiles == null) {
			return;
		}
		String currPath = webFiles.get(currIndex);

		loadUrl(ipAddr + Config.DOWNLOAD_URL_PART + currPath);

	}

	public void initView(List<String> webFiles, String ipAddr, int currIndex) {
		this.webFiles = webFiles;
		this.ipAddr = ipAddr;
		this.currIndex = currIndex;
		getViewByIndex();
	}

	public DynamicWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public int indexAdd() {
		if (webFiles != null && currIndex < webFiles.size() - 1) {
			currIndex++;
			getViewByIndex();
		}
		return 0;
	}

	@Override
	public int indexSub() {
		if (webFiles != null && currIndex > 0) {
			currIndex--;
			getViewByIndex();
		}
		return 0;
	}

}
